package datalookin.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.whu.zj.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/6/5.
 */
public class RealFragment extends Fragment {

    private ViewPager mPager;
    private ArrayList<Fragment> fragmentList;

    Button btn1,btn2;
    View indict1,indict2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_real,null);
        initview(view);
        return view;

    }

    View.OnClickListener onClickListener = new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            if (view == btn1)
            {
                mPager.setCurrentItem(0);
            }
            else if (view == btn2)
            {
                mPager.setCurrentItem(1);
            }
        }
    };

    private void initview(View view) {
        InitViewPager(view);
        btn1 = (Button) view.findViewById(R.id.id_btn1);
        btn2 = (Button) view.findViewById(R.id.id_btn2);

        btn1.setOnClickListener(onClickListener);
        btn2.setOnClickListener(onClickListener);

        indict2 = view.findViewById(R.id.id_indict_v2);
        indict1 = view.findViewById(R.id.id_indict_v1);
    }

    private void InitViewPager(View view) {
        mPager = (ViewPager)view.findViewById(R.id.viewpager);
        fragmentList = new ArrayList<Fragment>();
        Fragment btFragment= new RealPlantFragment();
        Fragment secondFragment = new RealStationFragment();
        fragmentList.add(0,btFragment);
        fragmentList.add(1,secondFragment);


        mPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentList));
        mPager.setCurrentItem(0);
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());
        mPager.setOffscreenPageLimit(1);

    }

    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageSelected(int arg0) {
            Log.i("selected",arg0+"");
            switch (arg0)
            {
                case 0:
                    btn1.setTextColor(getResources().getColor(R.color.bluelight));
                    btn2.setTextColor(getResources().getColor(R.color.grey));
                    indict1.setVisibility(View.VISIBLE);
                    indict2.setVisibility(View.GONE);
                    break;

                case 1:
                    btn1.setTextColor(getResources().getColor(R.color.grey));
                    btn2.setTextColor(getResources().getColor(R.color.bluelight));
                    indict1.setVisibility(View.GONE);
                    indict2.setVisibility(View.VISIBLE);
                    break;

            }
        }


    }

    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        ArrayList<Fragment> list;
        public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
            super(fm);
            this.list = list;

        }


        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Fragment getItem(int arg0) {
            Log.i("getItem",arg0+"");
            return list.get(arg0);
        }
    }
}
